package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"
	"mall/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAttrGroupByCategoryIdAndPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAttrGroupByCategoryIdAndPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAttrGroupByCategoryIdAndPageLogic {
	return &GetAttrGroupByCategoryIdAndPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetAttrGroupByCategoryIdAndPage 获取分类属性分组
func (l *GetAttrGroupByCategoryIdAndPageLogic) GetAttrGroupByCategoryIdAndPage(in *pb.GetAttrGroupByCategoryIdReq) (*pb.GetAttrGroupByCategoryIdResp, error) {
	list, err := l.svcCtx.AttrGroupModel.FindByCategoryIdAndPageAndKey(l.ctx, in.Key, in.Limit, in.PageIndex, in.CategoryId)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithCode(xerr.DbError), "err: %v", err)
	}
	resList := make([]*pb.AttrGroup, len(list))
	for i, v := range list {
		resList[i] = &pb.AttrGroup{
			AttrGroupId:   v.AttrGroupId,
			AttrGroupName: v.AttrGroupName,
			Sort:          v.Sort,
			Description:   v.Descript,
			Icon:          v.Icon,
			CateLogId:     v.Descript,
		}
	}
	return &pb.GetAttrGroupByCategoryIdResp{List: resList}, nil
}
